<template>
  <el-dialog title="出库产品选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="产品编码" prop="itemCode">
            <el-input
              v-model="queryParams.itemCode"
              placeholder="请输入产品编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="产品名称" prop="itemName">
            <el-input
              v-model="queryParams.itemName"
              placeholder="请输入产品名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="客户编码" prop="clientCode">
            <el-input
              v-model="queryParams.clientCode"
              placeholder="请输入客户编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="客户名称" prop="clientName">
            <el-input
              v-model="queryParams.clientName"
              placeholder="请输入客户名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="6">
          <el-form-item label="批次码" prop="batchCode">
            <el-input
              v-model="queryParams.batchCode"
              placeholder="请输入批次码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>

    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    <el-table
      v-loading="loading"
      :data="clientList"
      border
      @current-change="handleCurrent"
      @row-dblclick="handleRowDbClick">
      <el-table-column width="50" align="center" >
        <template v-slot="scope">
          <el-radio v-model="selectedClientId" :label="scope.row.lineId" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="产品编码" align="center" prop="itemCode" ></el-table-column>
      <el-table-column label="产品名称" align="center" prop="itemName" ></el-table-column>
      <el-table-column label="单位" align="center" prop="unitOfMeasure" ></el-table-column>
      <el-table-column label="规格型号" align="center" prop="specification" ></el-table-column>
      <el-table-column
        v-for="col in columnsConfig"
        :key="col.prop"
        :label="col.label"
        :prop="col.prop"
        :width="col.width"
        :align="col.align"
        v-if="col.show">
      </el-table-column>
      <el-table-column label="批次码" align="center" prop="batchCode" fixed="right"></el-table-column>
      <el-table-column label="客户编码" align="center" prop="clientCode" fixed="right"/>
      <el-table-column label="客户名称" width="150px" align="center" prop="clientName" fixed="right" :show-overflow-tooltip="true"/>
      <el-table-column label="出库数量" align="center" prop="quantitySalse" fixed="right" ></el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listItem } from "@/api/mes/qc/oqc";
import {getColumnsConfigKey} from "@/api/system/columnsconfig";

export default {
  name: "SalseSelect",
  data() {
    return {
      showFlag:false,
      selectedClientId: undefined,
      selectedRow: undefined,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      clientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemCode: null,
        itemName: null,
        batchCode: null,
        clientCode: null,
        clientName: null,
      },
      columnsConfig:[],
    };
  },
  created() {
    this.getList();
    this.getItemBase();
  },
  methods: {
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.clientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getItemBase(){
      getColumnsConfigKey('MdItem_Base').then(res=>{
        this.columnsConfig = JSON.parse(res.data);
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row){
      if(row){
        this.selectedRow = row;
      }
    },
    //行双击选中
    handleRowDbClick(row){
      if(row){
        this.selectedRow = row;
        this.$emit('onSelected',this.selectedRow);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRow = row;
      }
    },
    //确定选中
    confirmSelect(){
      if(this.selectedClientId === null || this.selectedClientId === 0){
        this.$notify({
          title:'提示',
          type:'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected',this.selectedRow);
      this.showFlag = false;
    }
  }
};
</script>
